<template>
  <div class="approval-record-item">
    <!-- 子流程分支 -->
    <div class="sub-process" v-if="hasChildren">
      <div class="branch-container">
        <div class="branch-body">
          <div
            v-for="(child, index) in item.child"
            :key="index"
            class="sub-process-item"
            :class="{'last-item': index === item.child.length - 1}"
          >
            <div class="branch-connector">
              <div class="branch-line-horizontal"></div>
            </div>
            <el-card :body-style="{ padding: '10px' }" class="sub-card">
              <div class="card-content">
                <div class="approver-info">
                  <label v-if="child.Approver">
                    审批人：{{ child.Approver }}
                  </label>
                </div>
                <div class="time-info">
                  <span v-if="child.SubmTime">
                    创建时间：<span class="time-value">{{ child?.SubmTime }}</span>
                  </span>
                  <span v-if="child.addtime && child.StateID!=='0'">
                    审批时间：<span class="time-value">{{ child?.addtime }}</span>
                  </span>
                </div>
                <p v-if="child.content" class="approval-comment"> 审批建议：{{ child.content }} </p>
                <div v-if="child.StateID" class="approval-status">
                  <el-tag :type="getStatusType(child.StateID)">{{ getStatusText(child.StateID) }}</el-tag>
                </div>
              </div>
            </el-card>
          </div>
        </div>
      </div>
    </div>

    <!-- 非子流程 -->
    <div v-else class="single-process">
      <el-card :body-style="{ padding: '10px' }" class="single-card">
        <div class="card-content">
          <div class="approver-info">
            <label v-if="item.userRomeName">
              审批人：{{ item.userRomeName }}
            </label>
          </div>
          <div v-if="item.SubmTime || item.addtime" class="time-info">
            <span v-if="item.SubmTime">
              创建时间：<span class="time-value">{{ item?.SubmTime }}</span>
            </span>
            <span v-if="item.addtime">
              审批时间：<span class="time-value">{{ item?.addtime }}</span>
            </span>
          </div>
          <p v-if="item.content" class="approval-comment"> 审批建议：{{ item.content }} </p>
          <div v-if="item.StateID" class="approval-status">
            <el-tag :type="getStatusType(item.StateID)">{{ getStatusText(item.StateID) }}</el-tag>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue';
import { parseTime } from "@/utils/util";

defineOptions({ name: 'ApprovalRecordItem' });

const props = defineProps({
  item: {
    type: Object,
    required: true
  }
});

const hasChildren = computed(() => {
  return props.item.child && props.item.child.length > 0;
});

// 获取状态类型
const getStatusType = (stateId) => {
  switch (stateId) {
    case '1': return 'success';
    case '2': return 'danger';
    default: return 'info';
  }
};

// 获取状态文本
const getStatusText = (stateId) => {
  switch (stateId) {
    case '1': return '已通过';
    case '2': return '已拒绝';
    default: return '处理中';
  }
};
</script>

<style scoped>
.approval-record-item {
  width: 100%;
}

.sub-process {
  margin: 10px 0;
  position: relative;
}

.branch-container {
  position: relative;
  padding-left: 20px;
  margin-left: 10px;
  border-left: 2px solid #409EFF;
}

.branch-body {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.sub-process-item {
  display: flex;
  position: relative;
  margin-bottom: 15px;
}

.branch-connector {
  position: absolute;
  left: -22px;
  top: 50%;
  display: flex;
  align-items: center;
}

.branch-line-horizontal {
  width: 20px;
  height: 2px;
  background-color: #409EFF;
}

.sub-card {
  flex: 1;
  width: 100%;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.single-process {
  margin-left: 10px;
}

.single-card {
  border-left: 2px solid #409EFF;
}

.card-content {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.approver-info {
  font-weight: normal;
  margin-bottom: 5px;
}

.time-info {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  font-weight: normal;
}

.time-value {
  color: #8a909c;
}

.approval-comment {
  margin-top: 5px;
  margin-bottom: 0;
}

.approval-status {
  margin-top: 5px;
}

.last-item {
  margin-bottom: 0;
}
</style>
